package com.collectplus.express.self;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.collectplus.express.R;
import com.collectplus.express.logic.DialogMgr;
import com.collectplus.express.model.ServicePointBean;
import com.collectplus.express.view.CircleImageView;

import java.util.ArrayList;
import java.util.HashMap;

import droid.frame.activity.base.BaseAdapterExt;
import droid.frame.utils.GlideUtil;
import droid.frame.utils.lang.Str;

/**
 * 自助寄件、投递点智能柜\驿站选择
 *
 * @author coffee <br>
 * 2016-9-24下午4:35:27
 */
public class SelfServicePointAdapter extends BaseAdapterExt<ServicePointBean> {

    /**
     * 选中的item的id
     */
    //    private int selected = -1;
    private boolean isStation = true;//true 为驿站  false 为柜子
    private HashMap<Integer, ServicePointBean> selectedMap = new HashMap<>();

    public SelfServicePointAdapter(ArrayList<ServicePointBean> items, Activity context) {
        super(items, context);
        setEmptyView(R.layout.self_service_point_empty);
        //初始化map集合
        if (items != null) {
            for (ServicePointBean bean : items) {
                selectedMap.put(bean.getId(), bean);
            }
        }
    }

    @Override
    public int getCount() {
        ServicePointBean bean = null;
        for (ServicePointBean item : getItems()) {
            if (item.isChecked()) {
                bean = item;
                break;
            }
        }
        //有选中的
        if (bean != null && bean.isChecked()) {
            if (isStation) {
                return super.getCount();
            } else {
                return 1;
            }
        } else {
            return super.getCount();
        }
    }

    /**
     * 设置选中
     */
    public void setSelected(ServicePointBean bean) {
        //点击选择柜子
        if (!isStation) {
            for (ServicePointBean item : getItems()) {
                if (item.getId() == bean.getId()) {
                    if (bean.isChecked()) {
                        bean.setChecked(false);
                        selectedMap.put(bean.getId(), bean);
                    } else {
                        bean.setChecked(true);
                        selectedMap.put(bean.getId(), bean);
                    }
                } else {
                    item.setChecked(false);
                    selectedMap.put(item.getId(), item);
                }
            }
        } else {
            for (ServicePointBean item : getItems()) {
                item.setChecked(false);
                selectedMap.put(item.getId(), item);
            }
            bean.setChecked(true);
            selectedMap.put(bean.getId(), bean);
        }
    }

    public void setStation(boolean isStation) {
        this.isStation = isStation;
    }

    public int getSelectedId() {
        int selectId = -1;
        if (getItems().size() > 0) {
            for (ServicePointBean bean : getItems()) {
                if (bean.isChecked()) {
                    selectId = bean.getId();
                    break;
                } else {
                    selectId = -1;
                }
            }
        }
        return selectId;
    }

    public ServicePointBean getSelectedItem(int id) {
        ServicePointBean item = null;
        for (ServicePointBean bean : getItems()) {
            if (bean.getId() == id) {
                item = bean;
                break;
            }
        }
        return item;
    }

    @Override
    public ServicePointBean getItem(int position) {
        ServicePointBean bean = null;
        if (isStation) {
            bean = super.getItem(position);
        } else {
            for (int j = 0; j < getItems().size(); j++) {
                ServicePointBean item = getItems().get(j);
                if (item.isChecked()) {
                    bean = item;
                    break;
                } else {
                    bean = super.getItem(position);
                }
            }
        }
        return bean;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (hasEmptyView()) {
            return super.getView(position, convertView, parent);
        }
        ViewHolder holder;
        if (convertView == null || convertView.getTag() == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.self_service_point_item, parent, false);
            holder = new ViewHolder();
            holder.icon = (CircleImageView) convertView.findViewById(R.id.self_service_icon);
            holder.name = (TextView) convertView.findViewById(R.id.self_service_name);
            holder.time = (TextView) convertView.findViewById(R.id.self_service_time);
            holder.address = (TextView) convertView.findViewById(R.id.self_service_address);
            holder.distance = (TextView) convertView.findViewById(R.id.self_service_distance);
            holder.selected = (CheckBox) convertView.findViewById(R.id.self_service_selected);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final ServicePointBean item = getItem(position);
        holder.name.setText(item.getName());

        // 驿站
        if (item.isStation()) {
            holder.time.setText(item.getServiceTime());
            holder.time.setVisibility(View.VISIBLE);
            if (Str.isNotEmpty(item.getHeadImage())) {
                GlideUtil.loadImage(context, item.getHeadImage(), 0, holder.icon);
            } else {
                holder.icon.setImageResource(R.drawable.toudi_yizhan_more);
            }


            holder.icon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (Str.isNotEmpty(item.getHeadImage())) {
                        DialogMgr.showLargeStationPhoto(context, item.getHeadImage());
                    }
                }
            });

        } else {
            holder.time.setVisibility(View.GONE);
            holder.icon.setImageResource(R.drawable.toudi_kuizi_moren);
        }
        holder.address.setText(item.getAddress());
        holder.distance.setText(item.getDistance() + "");
        holder.selected.setChecked(item.isChecked());
        return convertView;
    }

    private class ViewHolder {
        CircleImageView icon;
        TextView name;// 柜子名称
        TextView time;// 驿站--服务时间
        TextView address;// 柜子地址(详细地址)
        TextView distance;// 距离
        CheckBox selected;// 选中状态
    }
}
